package com.neusoft.clouduserfeign.controller;

import com.neusoft.clouduserfeign.dto.Employee;
import com.neusoft.clouduserfeign.dto.EmployeeVO;
import com.neusoft.clouduserfeign.dto.Result;
import com.neusoft.clouduserfeign.feign.EmployeeServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工服务Feign控制器
 * 通过负载均衡调用员工服务
 */
@RestController
@RequestMapping("/feign/employees")
public class EmployeeFeignController {

    @Autowired
    @Qualifier("com.neusoft.clouduserfeign.feign.EmployeeServiceClient")
    private EmployeeServiceClient employeeServiceClient;

    /**
     * 创建员工
     */
    @PostMapping
    public Result<Void> createEmployee(@RequestBody Employee employee, @RequestParam("createBy") String createBy) {
        return employeeServiceClient.createEmployee(employee, createBy);
    }

    /**
     * 更新员工信息
     */
    @PutMapping("/{userId}")
    public Result<Void> updateEmployee(@PathVariable("userId") String userId,
                                      @RequestBody Employee employee,
                                      @RequestParam("updateBy") String updateBy) {
        return employeeServiceClient.updateEmployee(userId, employee, updateBy);
    }

    /**
     * 删除员工（逻辑删除）
     */
    @DeleteMapping("/{userId}")
    public Result<Void> deleteEmployee(@PathVariable("userId") String userId, @RequestParam("updateBy") String updateBy) {
        return employeeServiceClient.deleteEmployee(userId, updateBy);
    }

    /**
     * 更新员工状态
     */
    @PutMapping("/{userId}/status")
    public Result<Void> updateEmployeeStatus(@PathVariable("userId") String userId,
                                            @RequestParam("status") String status,
                                            @RequestParam("updateBy") String updateBy) {
        return employeeServiceClient.updateEmployeeStatus(userId, status, updateBy);
    }

    /**
     * 根据用户ID获取员工详细信息（包含用户基本信息）
     */
    @GetMapping("/user/{userId}")
    public Result<EmployeeVO> getEmployeeByUserId(@PathVariable("userId") String userId) {
        return employeeServiceClient.getEmployeeByUserId(userId);
    }

    /**
     * 根据员工工号获取员工详细信息（包含用户基本信息）
     */
    @GetMapping("/no/{employeeNo}")
    public Result<EmployeeVO> getEmployeeByEmployeeNo(@PathVariable("employeeNo") String employeeNo) {
        return employeeServiceClient.getEmployeeByEmployeeNo(employeeNo);
    }

    /**
     * 查询员工详细信息列表（包含用户基本信息）
     */
    @GetMapping
    public Result<List<EmployeeVO>> getEmployeeList(@RequestParam(value = "deptName", required = false) String deptName,
                                                   @RequestParam(value = "status", required = false) String status,
                                                   @RequestParam(value = "keyword", required = false) String keyword) {
        return employeeServiceClient.getEmployeeList(deptName, status, keyword);
    }

    /**
     * 根据部门查询员工详细信息列表（包含用户基本信息）
     */
    @GetMapping("/dept/{deptName}")
    public Result<List<EmployeeVO>> getEmployeesByDept(@PathVariable("deptName") String deptName) {
        return employeeServiceClient.getEmployeesByDept(deptName);
    }

    /**
     * 根据上级ID查询下属员工详细信息列表（包含用户基本信息）
     */
    @GetMapping("/manager/{managerId}")
    public Result<List<EmployeeVO>> getEmployeesByManager(@PathVariable("managerId") String managerId) {
        return employeeServiceClient.getEmployeesByManager(managerId);
    }

    /**
     * 生成员工工号
     */
    @GetMapping("/generate-no")
    public Result<String> generateEmployeeNo() {
        return employeeServiceClient.generateEmployeeNo();
    }

    /**
     * 检查员工工号是否存在
     */
    @GetMapping("/check-no/{employeeNo}")
    public Result<Boolean> checkEmployeeNoExists(@PathVariable("employeeNo") String employeeNo) {
        return employeeServiceClient.checkEmployeeNoExists(employeeNo);
    }
}
